# define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>

struct ListNode {
    int val;
    struct ListNode* next;
};

struct ListNode* removeElements(struct ListNode* head, int val) {
    if (head == NULL)
    {
        return NULL;
    }
    while (head != NULL && head->val == val)
    {
        struct ListNode* tmp = head;
        head = head->next;
        free(tmp);
        tmp = NULL;
    }
    if (head == NULL)
    {
        return NULL;
    }
    struct ListNode* prev = head;
    while (head->next != NULL)
    {
        if (head->next->val == val)
        {
            struct ListNode* tmp = head->next;
            head->next = head->next->next;
            free(tmp);
            tmp = NULL;
        }
        else
        {
            head = head->next;
        }
    }
    return prev;
}


